﻿using log4net;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;

namespace IOA.MES.TcpService.WeldingService
{
    public class WeldingReceiveFilter : IReceiveFilter<BinaryRequestInfo>
    {
        private ILog Logger = LogManager.GetLogger(nameof(WeldingReceiveFilter));
        int IReceiveFilter<BinaryRequestInfo>.LeftBufferSize { get { return 0; } }

        IReceiveFilter<BinaryRequestInfo> IReceiveFilter<BinaryRequestInfo>.NextReceiveFilter { get { return new WeldingReceiveFilter(); } }

        FilterState IReceiveFilter<BinaryRequestInfo>.State { get { return FilterState.Normal; } }

        public void Reset() { }

        public BinaryRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
        {
            rest = 0;
            try
            {
                return new BinaryRequestInfo("", readBuffer.Skip(offset).Take(length).ToArray());
            }
            catch (Exception ex)
            {
                Logger.Fatal($"Filter异常：{ex.Message}", ex);
            }
            return new BinaryRequestInfo("", new List<byte>().ToArray());
        }
    }
}
